<template>
  <div style="background-color: rgba(240, 240, 240, 1);padding-top: 25px;">
    <timer></timer>
    <!-- <countdown></countdown> -->
    <div v-for="(item,index) in testlist" :key="index" style="margin-bottom: 0px;">
      <topic :topiceinfo='item' :topicindex='index' @returnkey='addkey'></topic>
    </div>
    <el-button style="margin: 5vw auto;margin-bottom: 40px;width: 90vw;border-radius: 8px;" type="primary"
      v-if="this.testlist.length" @click='submit()'>提交试卷
    </el-button>
  </div>
</template>

<script>
  import timer from './library/Timer.vue'
  // import countdown from './library/Countdown.vue'
  import topic from './library/Topiccard.vue'
  export default {
    name: 'exam',
    components: {
      topic,
      timer
    },
    data() {
      return {
        testUUId: '',
        testlist: [],
      }
    },
    methods: {
      addkey(opt) {
        let data = this.testlist.filter((item) => item.serialNum == (opt.num + 1));
        let mullist = ''
        if (data[0].questionType == 1) {
          if (opt.key.length == 0) {
            data[0].rightKey = [];
          } else {
            for (let i = 0; i < opt.key.length; i++) {
              mullist += String.fromCharCode(opt.key[i] + 65)
            }
            data[0].rightKey = mullist;
          }
        } else {
          data[0].rightKey = String.fromCharCode(opt.key + 65)
        }
        this.testlist[opt.num] = data[0];
        // console.log(this.testlist)
      },
      submit() {
        let isover = this.testlist.filter((item) => item.rightKey == null || item.rightKey.length == 0)

        if (isover.length == 0) {
          this.$confirm('已完成所有题目，是否提交试卷？', '提示', {
            confirmButtonText: '确定',
            customClass: 'message_box_alert',
            // type: '',
            center: true,
            showClose: false,
            closeOnClickModal: false
          }).then(() => {
            let ehoicerightkeyoption = this.testlist.filter((item) => item.questionType == 0)
            let multiplerightkeyoption = this.testlist.filter((item) => item.questionType == 1)
            let judgerightkeyoption = this.testlist.filter((item) => item.questionType == 2)
            // console.log(ehoicerightkeyoption)
            // console.log(multiplerightkeyoption)
            // console.log(judgerightkeyoption)
            let ehoicerightkeys = ehoicerightkeyoption.map(item => item.rightKey)
            let multiplerightkeys = multiplerightkeyoption.map(item => item.rightKey)
            let judgerightkeys = judgerightkeyoption.map(item => item.rightKey)
            // console.log('-------------------------------------------')
            // console.log(ehoicerightkeys)
            // console.log(multiplerightkeys)
            // console.log(judgerightkeys)
            const requestAnswerBody = {
              "judgmentAnswers": judgerightkeys,
              "multipleAnswers": multiplerightkeys,
              "singleAnswers": ehoicerightkeys,
              "testUuid": this.testUUId,
              "completionTime": this.$store.state.seconds,
            }
            this.$postRequest({
              url: '/scoreInfo/submitAnswer',
              params: requestAnswerBody,
              isJson: 1
            }).then(res => {
              console.log(res)
              if (res.code == 200) {
                this.$alert(res.msg, '提示', {
                  confirmButtonText: '确定',
                  customClass: 'message_box_alert'
                }).then(() => {
                  this.$router.go(-1);
                })
              } else {
                this.alert("服务器崩溃了，请稍后再试！");
              }
            })
          })

        } else {
          this.alert('还有题目未完成')
        }

      },
      alert(text) {
        this.$alert(text, '提示', {
          confirmButtonText: '确定',
          customClass: 'message_box_alert'
        })
      },
      gettestbody() {
        let data = {
          'testUUId': this.testUUId,
          // "completionTime": this.$store.state.time
        }
        this.$getRequest({
            url: '/testPaper/studentGetTestPaperBody',
            params: data
          }).then(res => {
            // console.log(res);
            if (res.code === 200) {
              this.testlist = res.data.questionList;
              if (res.data.questionList.length == 0) {
                this.$alert('暂无试题，点击确定返回', '提示', {
                  confirmButtonText: '确定',
                  customClass: 'message_box_alert'
                }).then(() => {
                  this.$router.replace({
                    path: '/'
                  });
                })
              }

            } else {
              this.$alert(res.msg, '提示', {
                confirmButtonText: '确定',
                customClass: 'message_box_alert'
              }).then(() => {
                this.$router.replace({
                  path: '/'
                });
              })

            }
          })
          .catch(failResponse => {});
      }
    },
    mounted() {
      document.title = this.$route.query.testName;
      this.testUUId = this.$route.query.testUuid;
      this.gettestbody();
    }
  }
</script>

<style>
  .message_box_alert {
    width: 80vw;
  }
 .el-message-box__btns{
    display: flex;
  }
  .el-button{
    width: 20%;
    flex: 2;
  }
</style>
